package com.parker.flowable;

import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class HolidayRequest {

    public static void main(String[] args) {
        //配置流程
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
                .setJdbcUsername("sa")
                .setJdbcPassword("")
                .setJdbcDriver("org.h2.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        ProcessEngine processEngine = configuration.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .deploy();

        ProcessDefinition processDefinition =repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();
        System.out.println("找到一个流程名字："+processDefinition.getName()+";流程Id："+processDefinition.getId());

        //发起流程
        Scanner scanner = new Scanner(System.in);
        System.out.println("你的名字");
        String employee = scanner.nextLine();

        System.out.println("你想请多久的假期？");

        Integer nrOfHolidays = Integer.valueOf(scanner.nextLine());

        System.out.println("你为什么需要假期？");
        String description = scanner.nextLine();


        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String,Object> variable = new HashMap<String,Object>();
        variable.put("employee",employee);
        variable.put("nrOfHolidays",nrOfHolidays);
        variable.put("description",description);
        ProcessInstance processInstance = runtimeService.startProcessInstanceById("holidayRequest:1:3",variable);

        //查看managers的流程
        TaskService taskService = processEngine.getTaskService();
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
        System.out.println("你有 "+tasks.size()+"项任务");
        tasks.forEach(task-> System.out.println(task.getName()));

        //按照流程进行完成
        System.out.println("你想完成哪项任务");
        int taskIndex = Integer.valueOf(scanner.nextLine());
        Task task = tasks.get(taskIndex-1);
        Map<String,Object> processVariables = taskService.getVariables(task.getId());

        System.out.println(processVariables.get("employee")+"想要请"+processVariables.get("nrOfHolidays")+"的假期，你是否同意？");

        //流程结束后唤起通知或其他操作
        boolean approved  = scanner.nextLine().toLowerCase().equals("y");
        variable = new HashMap<String,Object>();
        variable.put("approved",approved);
        taskService.complete(task.getId(),variable);

        //统计
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricActivityInstance> activityInstances = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processInstance.getId())
                .finished()
                .orderByHistoricActivityInstanceEndTime().asc()
                .list();
        activityInstances.forEach(activity-> System.out.println(activity.getActivityId()+"花费了" +activity.getDurationInMillis()+"毫秒") );
    }
}
